#include <header.h>

void my_read(int file_fd,char *buf,int len){
    int size = 0;
    while(size < len){
        ssize_t ret = read(file_fd,buf+size,len-size);
        size += ret;
    }
}

int main(){

    while(1){
        int file_fd = open("1.txt",O_RDWR);
        char buf[100] = {0};

        ssize_t ret =  read(STDIN_FILENO,buf,sizeof(buf));
        write(file_fd,&ret,sizeof(ssize_t));
        write(file_fd,buf,ret);
        close(file_fd); 
        
        bzero(buf,sizeof(buf));
        int file2_fd = open("2.txt",O_RDWR);
        ssize_t  num = 0;
        my_read(file2_fd,(char *)&num,sizeof(ssize_t));
        my_read(file2_fd,buf,num);
        printf("buf : %s\n",buf);
        ftruncate(file2_fd,0);
        close(file2_fd);
        // bzero(buf,sizeof(buf));
    }
    return 0;
}
